package 
{
	import laya.webgl.WebGL;
	import laya.display.Text;
	import laya.display.Stage;
	import laya.utils.Handler;
	import view.GameStart;
	import view.GameBg;
	import view.GameOver;
	import view.GamePause;
	import laya.display.Sprite;
	import view.Role;
	import laya.events.Event;
	import view.Enemy;
	import laya.utils.Pool;
	import view.Bulle;
	/**
	 * 游戏主逻辑
	 * @author
	 */
	public class GameManager{
		public var score:int;
		public var level:int;
		public var attackTime:int = 500;
		public static var bulleSpeed:Number = 10;
		private var hps: Array = [1, 4, 10];
		private var nums:Array = [1, 1, 2];
		private var speeds: Array = [1, 2, 3];
		private var attack: Array = [20, 35, 80];
		private var scores: Array = [1, 5, 20];

		private var start:GameStart;
		private var gameBg:GameBg;
		private var gameOver:GameOver;
		private var gamePause:GamePause;
		public var hero:Role;
		private var moveX:Number;
		private var moveY:Number;

		private static var instance:GameManager;
		public function GameManager(){
			GameDispatcher.getInstance().on(EventName.GAME_START,this,onGameInit);
			GameDispatcher.getInstance().on(EventName.GAME_OVER,this,onGameOver);
			GameDispatcher.getInstance().on(EventName.ADD_SCORE,this,onAddScore);
			GameDispatcher.getInstance().on(EventName.GAME_PAUSE,this,onPause);
			GameDispatcher.getInstance().on(EventName.GAME_RESUME,this,onResume);
		}

		public static function getInstance() : GameManager
		{
			if(instance == null)
			{
				instance = new GameManager();
			}
			return instance;
		}

		public function gameStart():void
		{
			LayerManager.init();
			start = new GameStart();
			start.popup();
		}

		private function onGameInit():void
		{
			score = 0;
			level = 1;
			onAddScore(0);

			start.close();
			LayerManager.roleLayer.removeChildren();

			hero ||= new Role();
			hero.hp = 100;
			hero.attack = 1;
			hero.pos(360,800);
			LayerManager.roleLayer.addChild(hero);

			gameBg ||= new GameBg();
			LayerManager.scenseLayer.addChild(gameBg);

			gamePause ||= new GamePause();
			LayerManager.uiLayer.addChild(gamePause);

			Laya.stage.on(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.stage.on(Event.MOUSE_UP,this,onMouseUp);

			Laya.timer.frameLoop(1,this,loop);
		}

		private function onMouseDown():void
		{
			moveX=Laya.stage.mouseX;
			moveY=Laya.stage.mouseY;
			Laya.stage.on(Event.MOUSE_MOVE,this,onMouseMove);
		}

		private function onMouseUp():void
		{
			Laya.stage.off(Event.MOUSE_MOVE,this,onMouseMove);
		}

		private function onMouseMove():void
		{
			var offsetX:Number = Laya.stage.mouseX - moveX;
			var offsetY:Number = Laya.stage.mouseY - moveY;
			hero.x += offsetX;
			hero.y += offsetY;
			moveX=Laya.stage.mouseX;
			moveY=Laya.stage.mouseY;
		}

		private function loop():void
		{
			checkCollision();
			if (Laya.timer.currFrame % (200) == 0)
			{
				createEnemy(1, hps[0], speeds[2], nums[2], attack[0],scores[0]);
			}
			if (Laya.timer.currFrame % (500) == 0)
			{
				createEnemy(2, hps[1], speeds[1], nums[1], attack[1],scores[1]);
			}
			if (Laya.timer.currFrame % (1000) == 0)
			{
				createEnemy(3, hps[2], speeds[0], nums[0], attack[2],scores[2]);
			}
		}

		private function createEnemy(index:Number,hp:int,speed:Number,num:int,attack:int ,score:int):void
		{
			for(var i:int = 0;i<num;i++)
			{
				var enemy:Enemy = Pool.getItemByClass("enemy", Enemy);
				var randomX:int = Math.random() *(720-80)+50;
				var randomY:int = -Math.random() * 100;
				enemy.init(randomX, randomY, index, speed, hp, attack, score);
			}
		}

		private function checkCollision():void
		{
			var enemy:Object;
			var tempEnemy:Object;
			var bulle:Object;
			for(var i:int = 0; i < LayerManager.roleLayer.numChildren;i++)
			{
				enemy = LayerManager.roleLayer.getChildAt(i);
				if(enemy is Enemy)
				{
					//发生碰撞
					if(enemy.isDie == false && Math.abs(hero.x-enemy.x) < (hero.hitRadius.x + enemy.hitRadius.x)/2 && Math.abs(hero.y-enemy.y) < (hero.hitRadius.y + enemy.hitRadius.y)/2)
					{
						hero.hit(enemy.attack);
						enemy.hit(hero.attack);
					}
				}
				bulle = LayerManager.roleLayer.getChildAt(i);
				if(bulle is Bulle)
				{
					for(var j:int = 0; j < LayerManager.roleLayer.numChildren;j++)
					{
						tempEnemy = LayerManager.roleLayer.getChildAt(j);
						if(tempEnemy is Enemy)
						{
							if(tempEnemy.isDie == false && Math.abs(bulle.x - tempEnemy.x) < (bulle.hitRadius.x + tempEnemy.hitRadius.x)/2 && Math.abs(bulle.y - tempEnemy.y) < (bulle.hitRadius.y + tempEnemy.hitRadius.y)/2)
							{
								bulle.hit();
								tempEnemy.hit(bulle.attack);
							}
						}
					}
				}
			}
		}

		private function onGameOver():void
		{
			Laya.stage.off(Event.MOUSE_DOWN,this,onMouseDown);
			Laya.stage.off(Event.MOUSE_UP,this,onMouseUp);
			Laya.stage.off(Event.MOUSE_MOVE,this,onMouseMove);
			Laya.timer.clear(this,loop);

			gameOver ||= new GameOver();
			gameOver.popup();
		}

		private function onAddScore(value:int):void
		{
			score += value;
			GameDispatcher.getInstance().event(EventName.SCORE_CHANGE);
		}

		private function onPause():void
		{
			Laya.timer.clear(this,loop);
		}

		private function onResume():void
		{
			Laya.timer.frameLoop(1,this,loop);
		}
	}

}